/*
 * @Descripttion: 
 * @version: 
 * @Author: ada.wu
 * @Date: 2023-10-19 16:02:52
 * @LastEditors: ada.wu
 * @LastEditTime: 2024-04-09 20:09:05
 */
import type { App, Plugin } from 'vue';
/**
 * 
 * @param name 获取url参数
 * @param href 
 * @returns 
 */
export function getUrlKey(name: any, href = "") {
    if (href == '') href = window.location.href
    let arr = new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(href) || [, ""];
    console.error('arr===',arr)
    // let urlValue = decodeURIComponent(arr[1].replace(/\+/g, '%20')) || null;
    let urlValue = decodeURIComponent(arr[1]) || null;
    if (urlValue == null) {
      name = name.toLowerCase();
      urlValue = decodeURIComponent(arr[1].replace(/\+/g, '%20')) || null;
    }
    return urlValue;
  }
/**
 * 科学防抖
 */
export const debounce = (function () {
    let timer = 0
    return function (callback: Function, ms: number) {
        clearTimeout(timer)
        timer = setTimeout(callback, ms)
    }
})()

/**
 * 一个简单的深度克隆 只克隆数据 过滤方法等
 * @param value
 */
export const cloneDeep = (value: any) => JSON.parse(JSON.stringify(value))

export const withInstall = <T>(component: T, alias?: string) => {
    const comp = component as any;
    comp.install = (app: App) => {
        app.component(comp.name || comp.displayName, component);
        if (alias) {
            app.config.globalProperties[alias] = component;
        }
    };
    return component as T & Plugin;
};


export const getPathWxMpAppId = () => {
    let pathname = window.location.pathname
    let path = pathname.substring("/coupon/".length, pathname.length)
    let appid = path.substring(0, path.indexOf("/"))
    if (!appid) {
        return ``
    }
    if (appid.indexOf("wx") === 0 && appid.length > 10) {
        return appid
    }
    return ``
}
export const getHostWxMpAppId = () => {
    // if (import.meta.env.VITE_APP_WX_MP_APP_ID) {
    //     return import.meta.env.VITE_APP_WX_MP_APP_ID
    // }

    let hostname = window.location.hostname
    let hostAppid = hostname.substring(0, hostname.indexOf(".ins10.cn"))
    return hostAppid
}

export const getAssets = (localPath: string | null) => {
    if (!localPath) {
        return null
    }
    return new URL(`../assets/${localPath}`, import.meta.url).href;
}
/**
 * 还原文件地址
 * @param fileId
 */
export const getFileUrl = (fileId: string | null) => {
    if (!fileId) {
        return null
    }
    return import.meta.env.VITE_APP_JINGWEI_URL + `${fileId}`;
}

export function uuid() {
    function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }

    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

export function getNewObj(val: any) {
    let resObj = JSON.stringify(val);
    return JSON.parse(resObj);
}
